// pages/order/orderConfirmInfo/orderConfirmInfo.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    work_order_id: "",
    type: 1,//1、测试确认单、2保管清单、3分享确认单
    work_order_type: 1,//1试用2改造

    info: '',
    is_sign: false,//是否确认完了，是否能签字
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var work_order_id = options.work_order_id||'';
    var type = options.type || 1;
    var work_order_type = options.work_order_type || 1;
    that.setData({
      work_order_id,
      type,
      work_order_type,
    })
    console.log(type,"type")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.getInfo();
  },
  //收起展开
  openFun(e){
    let { index } = e.currentTarget.dataset;
    let cookerArr = this.data.info.cooker_data;
    cookerArr[index].open = !cookerArr[index].open;
    let info = this.data.info;
    info.cooker_data = cookerArr;
    this.setData({ 
      info:info
     });
  },
  //获取确认单详情
  getInfo() {
    var that = this;
    app.ajax({
      url: 'Engineer/Workorder/testDataCookerDetail',
      method: "POST",
      data: {
        work_order_id: that.data.work_order_id,
        type: that.data.type,
        work_order_type: that.data.work_order_type,
      },
      success: function (res) {
        if (res.data.code == 1000) {
          var data = res.data.data;
          that.setData({
            info: data
          })
          if(that.data.type==2){
            var sureCount = 0;//累计已确认的数量
            var dataList = data.cooker_data.data;
            dataList.forEach((item) => {
              if (item.is_confirm == 2) {
                sureCount += 1;
              }
            })
            if (sureCount == dataList.length) {
              that.setData({
                is_sign: true
              })
            }
          }
          
        }
      }
    })
  },

  
  //去签字
  sign(){
    var that = this;
    var work_order_id = that.data.work_order_id;
    var type = that.data.type;//1、测试确认单、2保管清单、3分享确认单
    var work_order_type = that.data.work_order_type;//1试用2改造
    wx.navigateTo({
      url: '../sign/sign?work_order_id=' + work_order_id + '&type=' + type + '&work_order_type=' + work_order_type,
    })
  },

  // 查看大图
  previewImage(e) {
    var currentImg = e.currentTarget.dataset.url;
    wx.previewImage({
      current: currentImg, // 当前显示图片的http链接
      urls: [currentImg] // 需要预览的图片http链接列表
    })
  },
})